using System;

using System.Reflection;
using System.ComponentModel;
using CUtil;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace CStock
{
	
	/// <summary>
	/// Summary description for StockObjectBase.
	/// </summary>
    public class StockObjectBase : XMLSerializable, IStockObjectBase, ICloneable
	{
        

		//[System.Xml.Serialization.XmlIgnoreAttribute()]
		
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		private IList<double> items;

        [System.Xml.Serialization.XmlIgnoreAttribute()]
		protected IStockObjectBase parent;

		public StockObjectBase(IStockObjectBase myParent)
		{
			this.parent = myParent;
			//keep empty constructor - the overriden constructor sets the collection:
			this.items = new List<double>();
		}
        public StockObjectBase()
		{
			//keep empty constructor - the overriden constructor sets the collection:
			this.items = new List<double>();
		}

        public void SetPath(string myPath)
        {
            this.path = myPath;
        }

        #region former StockObject

        /// <summary>
        /// FocusedSubSelectionChanged
        /// </summary>
        //[System.Xml.Serialization.XmlIgnoreAttribute()]

        [Browsable(false)]
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public IStockObjectBase Parent
        {
            get
            {
                return this.parent;
            }

        }

        public void SetIDFromLongFileName(string myfileName)
        {

            string[] arrStr = myfileName.Split(new Char[] { '\\' }, 100);

            string myLongName = arrStr[arrStr.GetLength(0) - 1];

            string[] newArr = myLongName.Split(new Char[] { '.' }, 100);

            if (newArr.GetLength(0) > 0)
            {
                this.iD = newArr[0];
                
            }

        }
        public override bool Equals(object obj)
        {
            StockObjectBase objectStock = null;

            if (base.Equals(obj))
            {
                return true;
            }

            else
            {
                try
                {
                    if (obj != null)
                    {
                        objectStock = obj as StockObjectBase;
                        if (objectStock != null)
                        {
                            return this.iD.Equals(objectStock.iD);
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                catch (Exception exc)
                {
                    Debug.WriteLine("Error casting StockObjectBase: " + exc.Message);
                    return false;
                }

            }
            return false;

        }

        #endregion

        public override string ToString()
        {
            return this.iD;

        }
        #region ICloneable Members

        /// <summary>
        /// take care : each object should implement this!!
        /// </summary>
        /// <returns></returns>
        public virtual object Clone()
        {
            // create new settings class and copy all public properties values to clone

            object s = null;
            try
            {

                Type t = this.GetType();
                Assembly ass = t.Assembly;

                string className = t.Name;
                s = ReflectionClass.CreateInstanceOfClassFromAssembly(ass, className);

                System.Reflection.PropertyInfo[] piArr = s.GetType().GetProperties();


                foreach (System.Reflection.PropertyInfo pi in piArr)
                {
                    if (pi.GetSetMethod() != null)
                    {
                        //ignore errors
                        //for instance the this property (indexer) cannot be set
                        try
                        {
                            System.Reflection.PropertyInfo pOld = this.GetType().GetProperty(pi.Name);

                            pi.SetValue(s, pOld.GetValue(this, null), null);

                            //pi.SetValue(s, this.GetType().GetProperty(pi.Name).GetValue(this, null), null);
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Debug.WriteLine(e.Message);
                            //ignore exceptions!
                        }
                    }

                }
            }
            catch (Exception exc)
            {
                Debug.Assert(false, "Error cloning object " + this.iD, exc.Message);
            }
            return s;
        }

        #endregion

        public virtual bool Delete()
        {
            try
            {
                string fileName = this.path + "\\" + this.iD + ".xml";
                if (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                    return true;
                }
                return false;
                

            }
            catch (Exception ex)
            {
                Debug.Assert(false, "Error deleting " + this.iD + "; " + ex.Message);
                return false;
            }

        }
        //sorts the input list after name
        public static System.Collections.ArrayList SortObjects(ArrayList arrList)
        {
            try
            {
                SortedList sortedListOfNames = new SortedList();
                //creates a sorted list containing all names
                for (int i = 0; i < arrList.Count; i++)
                {
                    object c = ((StockObjectBase)arrList[i]).Name;
                    if (!sortedListOfNames.ContainsValue(c))
                        sortedListOfNames.Add(c.ToString(), c);
                }

                ArrayList arrListResult = new ArrayList();
                for (int i = 0; i < sortedListOfNames.Count; i++)
                {
                    //gets the sorted index
                    object c = sortedListOfNames.GetByIndex(i);
                    string indName = c.ToString();
                    for (int j = 0; j < arrList.Count; j++)
                    {
                        if(indName == ((StockObjectBase)arrList[j]).Name)
                            arrListResult.Add(arrList[j]);
                    }
                    
                }
                return arrListResult;

            }
            catch (Exception exc)
            {
                Debug.Assert(false, "Error sorting array list", exc.Message);

            }
            return arrList;


        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

   
	}


    public enum TradeEventEnum
    {
        MoneyTransfer
    }



}
